home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17801 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  52 lines

  1. Path: chopin.ecs.umass.edu!ktoutire
  2. From: ktoutire@chopin.ecs.umass.edu (Kiran K Toutireddy)
  3. Newsgroups: comp.lang.c++
  4. Subject: INITIALIZATION of a MEMBER CLASS of a CLASS
  5. Date: 17 Apr 1996 16:54:27 GMT
  6. Organization: University of Massachusetts, Amherst
  7. Message-ID: <4l37o3$iug@risky.ecs.umass.edu>
  8. NNTP-Posting-Host: chopin-gw.ecs.umass.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12.  
  13. Hi,
  14.     I have a question related to initializing the member classes inside a class data structure. The following example should explain the question.
  15.  
  16. I have a class called Engine, which can be initialized to Gas engine,Deisel Engine, Electric Engine, etc. by default, it is electric engine.(default constructor).
  17.  
  18. I have a totally unrelated class(no inheritances...) called Car.
  19.  
  20.     Now this Car class has a member 
  21.  
  22.         Engine car_engine;
  23.  
  24.     Since all the cars I consider have only gas engines( no EVs considered :-),
  25.     I want to initialize the car_engine member to be Gas engine by default, instead of the default electric engine. How do I do that?
  26.     the following is some pseudo code of the above example
  27.  
  28.     class Engine{
  29.         nuts..bolts..pipes..rods...;
  30.     public:
  31.         Engine(){  make this an electric engine,....;}
  32.         Engine(int Gas){ make it a gas engine..}
  33.         Engine(float Deisel){ make it a deisel engine;}
  34.     };
  35.  
  36.     
  37.     class Car{
  38.         Engine car_engine;
  39.     public:
  40.         Car(){ how do i initialize the car_engine to be of type Gas here?;}
  41.     };
  42.  
  43. I tried initializing the car_engine by saying Engine car_engine(int Gas), but it gives a warning saying that ANSI c++ forbids such declarations. 
  44.  
  45. Can anybody suggest a better way of doing this?
  46. thanks in advance
  47. -kiran
  48.  
  49.  
  50.  
  51.  
  52.